﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for FilmSchedule
/// </summary>
public class FilmSchedule
{
    public FilmSchedule()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public DataSet LoadAllFilm()
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();

        ds = db.LoadAllFilm();

        return ds;
    }

    public Boolean DeleteFilm(int iFilmId)
    {
        ConnectDB db = new ConnectDB();
        Boolean iResult = db.DeleteFilm(iFilmId);
        return iResult;
    }

    public DataSet LoadFilmSchedule(DateTime dateFilmShow, int iCinema)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.LoadFilmSchedule(dateFilmShow, iCinema);

        #region create table and row
        //ds.Tables[0].Columns["TimeShow1"].DataType = Type.GetType("System.Date");
        //string str = String.Format("{0:HH:mm}", Convert.ToDateTime(ds.Tables[0].Rows[0][1].ToString()));

        //DataTable dt = new DataTable();
        //dt = MakeTableFilm();

        //DataRow dr = dt.NewRow();

        ////dr["FilmName"] = ds.Tables[0].Rows[0][0].ToString();
        //dr[0] = ds.Tables[0].Rows[0][0].ToString();
        //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //    dr["RateMovie" + i.ToString()] = ds.Tables[0].Rows[i][1].ToString();
        //dt.Rows.Add(dr);
        #endregion

        return ds;
    }

    public DataSet LoadFilmENSchedule(DateTime dateFilmShow, int iCinema)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.LoadFilmEnSchedule(dateFilmShow, iCinema);

        #region create table and row
        //ds.Tables[0].Columns["TimeShow1"].DataType = Type.GetType("System.Date");
        //string str = String.Format("{0:HH:mm}", Convert.ToDateTime(ds.Tables[0].Rows[0][1].ToString()));

        //DataTable dt = new DataTable();
        //dt = MakeTableFilm();

        //DataRow dr = dt.NewRow();

        ////dr["FilmName"] = ds.Tables[0].Rows[0][0].ToString();
        //dr[0] = ds.Tables[0].Rows[0][0].ToString();
        //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //    dr["RateMovie" + i.ToString()] = ds.Tables[0].Rows[i][1].ToString();
        //dt.Rows.Add(dr);
        #endregion

        return ds;
    }

    private DataTable MakeTableFilm()
    {
        // Create a new DataTable titled 'film.'
        DataTable filmTable = new DataTable("Film");

        // Add three column objects to the table.
        DataColumn filmNameCol = new DataColumn();
        filmNameCol.DataType = System.Type.GetType("System.String");
        filmNameCol.ColumnName = "FilmName";
        filmTable.Columns.Add(filmNameCol);

        for (int i = 0; i < Int32.Parse(ConfigurationManager.AppSettings.GetValues("numberOfMovieRate")[0]); i++)
        {
            DataColumn rateMovie = new DataColumn();
            rateMovie.DataType = System.Type.GetType("System.String");
            rateMovie.ColumnName = "RateMovie" + i.ToString();
            rateMovie.DefaultValue = "";
            filmTable.Columns.Add(rateMovie);
        }

        // Create an array for DataColumn objects.
        DataColumn[] keys = new DataColumn[1];
        keys[0] = filmNameCol;
        filmTable.PrimaryKey = keys;

        // Return the new DataTable.
        return filmTable;

    }

    public Boolean AddNewFilm(Film f)
    {
        ConnectDB db = new ConnectDB();
        //set ID for new film
        DataSet ds = new DataSet();
        ds = db.MaxFilmID();
        int id = 0;
        if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString()!= "")
            id = Int32.Parse(ds.Tables[0].Rows[0][0].ToString());
        id++;
        f.IFilmID = id;
        Boolean iResult = db.AddFilm(f);
        return iResult;
    }

    public Boolean EditFilm(Film f)
    {
        ConnectDB db = new ConnectDB();
        Boolean iResult = db.EditFilm(f);
        return iResult;
    }

    public DataSet LoadFilmById(Int32 id)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.LoadFilmById(id);
        return ds;
    }
}
